package com.gy.api.mobile.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import com.gy.api.base.*;
import com.gy.api.mobile.user.domain.po.MobileUserPO;
import com.gy.api.mobile.user.domain.vo.*;

import com.gy.api.mobile.user.service.IUserService;
import com.gy.api.sms.req.SendCodeValidateVO;
import com.gy.api.sms.service.DynamicMessageService;
import com.gy.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/h5/user")
@Api( tags = ApiIndex.H5_UserController)
@Slf4j
public class MobileUserController {

    @Autowired
    private IUserService service;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private DynamicMessageService smsService;



    @ApiOperation("用户登录_密码登录[密码需要加密]")
    @PostMapping("/login_password")
    public R<UserLoginResVO> login_password(@RequestBody UserLoginVO userLoginVO){
        UserLoginResVO resVO=service.login_password(userLoginVO);
        return new R(resVO);
    }

    @ApiOperation("用户登录_验证码登录[短信登陆如果没有账号直接注册一个]")
    @PostMapping("/login_code")
    public R<UserLoginResVO> login_code(@RequestBody UserLoginCodeVO userLoginVO) throws Exception {
        UserLoginResVO resVO=service.login_code(userLoginVO);
        return new R(resVO);
    }


    @ApiOperation("退出登录")
    @PostMapping("/login_out")
    @SaCheckLogin
    public R loginOut(){
        StpUtil.logout();
        return new R();
    }


    @ApiOperation("验证短信验证码")
    @PostMapping("/validate_code")
    public R senCode(@ApiParam(value = "参数")  @RequestBody SendCodeValidateVO validateVO) throws Exception {
        String code= redisUtils.get(Constants.CAPTCHA_CODE_KEY +"_"+validateVO.getPhone()+"_"+validateVO.getSmsType());
        if(StringUtils.isEmpty(code)){
            throw new BusinessException(Type.FAIL.getCode(),"短信验证码不正确或已失效！");
        }
        if(validateVO.getSmsType()==2){
            MobileUserPO sysUser=service.selectUserByPhone(validateVO.getPhone());
            if(sysUser==null){
                 new BusinessException(Type.FAIL.getCode(),"该手机号码还未注册！");
            }
        }
        if(!code.equals(validateVO.getCode())){
            throw new BusinessException(Type.FAIL.getCode(),"短信验证码不正确！");
        }
        redisUtils.delete(Constants.CAPTCHA_CODE_KEY +"_"+validateVO.getPhone()+"_"+validateVO.getSmsType());
        return new R();
    }



    @ApiOperation("查询当前用户信息")
    @GetMapping("/one")
    @SaCheckLogin
    public R<UserVO> getOne(){
        UserVO userVO=service.getOne();
        return new R(userVO);
    }

    @ApiOperation("更新用户名称")
    @PostMapping("/update_name")
    @SaCheckLogin
    public R updateName(@RequestBody UpdateUserNameVO userNameVO){
        service.updateName(userNameVO);
        return new R();
    }

    @ApiOperation("更新用户密码(通过旧密码)")
    @PostMapping("/update_password")
    @SaCheckLogin
    public R updatePassword(@RequestBody UpdateUserPasswordVO updateUserPasswordVO) throws Exception {
        service.updateUserPasswordVO(updateUserPasswordVO);
        return new R();
    }

    @ApiOperation("通过手机更新用户密码")
    @PostMapping("/update_password_by_phone")
    public R update_password_by_phone(@RequestBody UpdateUserPasswordPhoneVO phoneVO) throws Exception {
        service.updateUserPasswordPhone(phoneVO);
        return new R();
    }

    @ApiOperation("换绑手机号码")
    @PostMapping("/binding_phone")
    @SaCheckLogin
    public R bindingPhone(@RequestBody UserCodeVO userCodeVO){
        service.bindingPhone(userCodeVO);
        return new R();
    }

    @ApiOperation("发送短信验证码")
    @PostMapping("/sen_code")
    public R senCode(@ApiParam(value = "参数")  @RequestBody SendCodeVO sendCodeVO) throws Exception {
        if(sendCodeVO.getSmsType()==2){
            MobileUserPO sysUser=service.selectUserByPhone(sendCodeVO.getPhone());
            if(sysUser==null){
                new BusinessException(Type.FAIL.getCode(),"该手机号码还未注册！");
            }
        }
        String code= RandomUtil.randomNumbers(4);
        log.info(sendCodeVO.getPhone()+":::::发送短信验证码::::"+code);
        redisUtils.set(Constants.CAPTCHA_CODE_KEY +"_"+sendCodeVO.getPhone()+"_"+sendCodeVO.getSmsType(),code,60*30, TimeUnit.SECONDS);
        try{

            switch (sendCodeVO.getSmsType()){
                case 1:
                    smsService.sendDynamicMessage("【光影重庆】您好，您此次验证码为:"+code+"，请不要把验证码泄漏给其他人，如非本人请勿操作。",sendCodeVO.getPhone());
                    break;
                case 2:
                    smsService.sendDynamicMessage("【光影重庆】您正在找回密码，验证码为："+code+"，请不要把验证码泄露给其他人。如非本人操作，可不用理会！",sendCodeVO.getPhone());
                    break;
                case 3:
                    smsService.sendDynamicMessage("【光影重庆】手机号变更验证码："+code+"，请勿泄露。如非本人操作，可不用理会！",sendCodeVO.getPhone());
                    break;
                case 4:
                    smsService.sendDynamicMessage("【光影重庆】手机绑定验证码为"+code+"，请不要把验证码泄露给其他人。如非本人操作，可不用理会！",sendCodeVO.getPhone());
                    break;
            }

        }catch (Exception e){
//            e.printStackTrace();
            return  new R(code);
        }
        return new R();
    }


}
